home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Technotools
/
Technotools (Chestnut CD-ROM)(1993).ISO
/
lang_c
/
msqc25t1
/
sub1.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-09-07
|
7KB
|
276 lines
/* sub1.c: A subdirectory listing utility. Enhancement of DOS DIR command.
Features of this program:
* All files and their attributes, plus time stamps.
* The number of files in each file and the space in K allocated to
it as a function of cluster size.
* Number of files in the directory, total bytes, and total space
taken by all files in K.
* Free space remaining on the disk.
Procedure:
1. Get and remember the current directory.
2. Find out what directory to search.
3. List the specified contents of the target directory.
4. Show summary infomation.
5. Restore the original directory.
6. Quit.
*/
#include <stdio.h>
#include <dos.h>
#include <io.h>
#include <conio.h>
#include <stdlib.h>
#include <direct.h>
#include <string.h>
#include <malloc.h>
#include <graph.h>
#include "filemisc.h"
#ifndef TRUE
#define FALSE 0
#define TRUE !FALSE
#endif
/* Globals */
char *olddir, searchpath[80],
filemask[13] = "*.*";
unsigned bps, nfiles, olddrive, drive;
long totalbytes, totalk;
int thru;
struct diskfree_t diskinfo;
main(int argc, char *argv[])
{
unsigned ndrives;
void listfiles (char*, char*), check (char*),
separate (char*, char*, char*);
/* Initialize */
olddir = getcwd (NULL, 80); /* get current directory */
_dos_getdrive (&olddrive); /* and drive */
totalbytes = totalk = 0L;
nfiles = 0;
drive = 0;
thru = FALSE;
strcpy (searchpath, olddir); /* default search path */
/* Get desired search path, change drives if necessary */
if (argc>1)
strcpy (searchpath, argv[1]);
if (searchpath[1] == ':') {
drive = (toupper (searchpath[0])) - 64;
_dos_setdrive (drive, &ndrives);
}
/* Get info about selected drive */
_dos_getdiskfree (drive, &diskinfo); /* drive info */
bps = diskinfo.sectors_per_cluster * /* blocksize */
diskinfo.bytes_per_sector;
/* Show all files in current dir if so command-line arg */
if (argc == 1) {
listfiles (searchpath, filemask);
thru = TRUE;
}
/* Is command SUB <dir> or SUB <\DIR>? */
if (!thru) {
strcpy (searchpath, argv[1]);
if(chdir (searchpath) == 0) {
listfiles (searchpath, filemask);
thru = TRUE;
}
}
/* Is command SUB <dir\*.*>? */
if (!thru) {
separate (argv[1], searchpath, filemask);
if (chdir (searchpath) == 0)
if (strlen (filemask) > 0) {
check (filemask);
listfiles (searchpath, filemask);
thru = TRUE;
}
}
/* Is command SUB *.*? */
if (!thru) {
strcpy (filemask, argv[1]);
check (filemask);
listfiles (olddir, filemask);
thru = TRUE;
}
/* Have we exhausted all possiblities? */
if (!thru)
puts ("PATH NOT FOUND");
/* Clean up and quit */
_dos_setdrive (olddrive, &ndrives); /* restore drive */
chdir (olddir); /* and directory */
free (olddir);
} /* END OF MAIN */
void check (char *mask)
/* Check file mask, complete with wildcards if necessary */
{
char temp[13];
if(mask[0] == '.') { /* make .* into *.* */
sprintf(temp, "*%s", mask);
strcpy (mask, temp); /* add wildcard */
}
}
void separate (char *arg, char *path, char *mask)
/* Break out path and mask from command-line arg */
{
char *brk;
strcpy (path, arg);
brk = strrchr (path, '\\'); /* find last '\' in path */
if (brk) { /* if found */
strcpy (mask, &(brk[1])); /* copy from right to mask */
*brk = '\0'; /* chop off mask leaving path */
}
}
void listfiles (char *path, char *mask)
/* List files in currently selected directory using mask */
{
struct find_t *file;
char *heading;
unsigned linecount, rc;
unsigned startpage (char*);
void writeinfo (struct find_t*);
void showtotals (void);
unsigned countlines (char*, unsigned);
/* Get space for working objects */
file = malloc (sizeof *file);
heading = malloc(160);
/* Begin report */
sprintf(heading, "Directory for %s in %s:", mask, path);
linecount = startpage (heading); /* first page */
/* Write report */
rc = _dos_findfirst (mask, 0xFF, file);
while (rc == 0) {
writeinfo (file);
/* linecount = countlines (heading, linecount); */
rc = _dos_findnext (file);
}
showtotals ();
}
unsigned startpage (char *header) /* Begin output page */
{
/* _clearscreen (_GCLEARSCREEN); */
puts (header);
printf("%-13s %-11s %-12s %-9s %-3s",
"Name", "Date", "Time", "Attrib", "Bytes");
return 3L;
}
unsigned countlines (char *head, unsigned count)
/* Count line, start new page if full */
{
++count;
if (count == 24) {
printf ("\n-- MORE --");
getch();
count = startpage (head);
}
return count;
}
void showtotals (void) /* End report with totals */
{
long free, size;
/* Compute totals */
free = (long) diskinfo.avail_clusters * bps;
size = (long) diskinfo.total_clusters * bps;
/* Report results */
printf ("\n%u files, %lu bytes, %luK space",
nfiles, totalbytes, totalk);
printf("\n%lu bytes free out of %lu", free, size);
}
void writeinfo (struct find_t *f) /* list file entry */
{
char tstring[12], dstring[11], *attribs (char);
unsigned kbytes (long), kb;
/* Get file info */
datestamp (f->wr_date, dstring, NULL, NULL, NULL);
timestamp (f->wr_time, tstring, NULL, NULL, NULL);
kb = kbytes (f->size);
/* Non-directories shown in lower case */
if (!(f->attrib & _A_SUBDIR)) strlwr (f->name);
/* Non-directory info */
printf("\n%-14s", f->name); /* name */
printf("%-12s", dstring); /* date */
printf("%-13s", tstring); /* time */
printf("%-7s", attribs (f->attrib)); /* attributes */
printf("%8lu", f->size); /* size in bytes */
/* printf("%6u", kb); */ /* size in k */
/* Update sums for this entry */
totalk += kb;
totalbytes += f->size;
++nfiles;
}
char *attribs (char attrib)
/* convert attribute byte to string */
{
static char attr[7];
strcpy (attr, "......");
if (attrib & _A_RDONLY) attr[5] = 'R';
if (attrib & _A_HIDDEN) attr[4] = 'H';
if (attrib & _A_SYSTEM) attr[3] = 'S';
if (attrib & _A_VOLID) attr[2] = 'V';
if (attrib & _A_SUBDIR) attr[1] = 'D';
if (attrib & _A_ARCH) attr[0] = 'A';
return attr;
}
unsigned kbytes (long bytes)
/* convert size to K, rounding up for cluster */
{
unsigned K = 0;
long clust;
if (bytes > 0L) { /* if file not empty */
clust = bytes / bps; /* number of clusters */
if ((clust % bps) != 0L) ++clust; /* round up */
if (clust == 0L) clust = 1; /* minimum of one */
K = (clust * bps) / 1024L; /* space in K */
}
return K;
}